﻿using System;
using System.Collections.Generic;
using Bing.Maps;
using GalaSoft.MvvmLight.Messaging;
using Mutzl.Samples.MapApp.Common;
using Mutzl.Samples.MapApp.Model;
using Mutzl.Samples.MapApp.ViewModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using System.Linq;

// Todo 07. Design der Pushpins als DataTemplate im XAML festlegen
// Z.B. "FinanzamtTemplate" ab Zeile 19
// oder "SonderObjektTemplate" ab Zeile 50
// man beachte die Verwendung der attached Properties 
//      MapExtensions.Location bzw. MapExtensions.Latitude und MapExtensions.Longitude
// Außerdem: das Tapped Event ruft in Zeile 24 dank einem EventToCommandBehavior ein Command auf.
//      Um die reinen Modelklassen (hier: Finanzamt) nicht mit Commands zu "verunreinigen",
//      ist das Command Teil des ViewModels (daher das ungewöhnliche Binding) 
//      und es wird ein Parameter (KurzZeichen) des auslösenden Objektes mitgegeben.

namespace Mutzl.Samples.MapApp.View
{
    public sealed partial class MapView
    {
        public MapView()
        {
            InitializeComponent();

            ViewModel.Messenger.Register<NotificationMessage<LocationCollection>>(this, ZoomToFitAll);


            ViewModel.PropertyChanged += (sender, args) =>
                                             {
                                                 if (args.PropertyName == "HideAppBars") CloseAppBars();
                                             };
        }

        private void CloseAppBars()
        {
            if (BottomAppBar != null) BottomAppBar.IsOpen = false;
            if (TopAppBar != null) TopAppBar.IsOpen = false;
        }


        private void GoToDetail(NotificationMessage<object> msg)
        {
            var id = msg.Content as string;
            Frame.Navigate(typeof(DetailView), id);
        }


        protected override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            Messenger.Default.Register<NotificationMessage<object>>(this, true, GoToDetail);
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatingFrom(Windows.UI.Xaml.Navigation.NavigatingCancelEventArgs e)
        {
            Messenger.Default.Unregister<NotificationMessage<object>>(this);
            base.OnNavigatingFrom(e);
        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private MapViewModel ViewModel { get { return DataContext as MapViewModel; } }


        private void ZoomToFitAll(NotificationMessage<LocationCollection> message)
        {
            ZoomToFitAll(new LocationRect(message.Content));
        }

        private void ZoomToFitAll(LocationRect boundingRectangle)
        {
            MapControl.SetView(boundingRectangle);
        }
    }
}
